﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Forums.OfflineStore;
using System.Data.Linq;

namespace Microsoft.Forums.Tests.HelperTools
{
    /// <summary>
    /// Helper methods for tests to easily use the database
    /// </summary>
    public static class DatabaseHelper
    {

        /// <summary>
        /// Returns the connection string
        /// </summary>
        public static string ConnectionString
        {
            get
            {
                return string.Format("Data Source={0}", DatabaseFileName);
            }
        }

        public static string DatabaseFileName
        {
            get
            {
                return Properties.Resources.DatabaseFileName;
            }
        }

        /// <summary>
        /// Creates the database if it does not exist
        /// </summary>
        public static void SetupDatabase()
        {
            if (DatabaseUtilities.DoesDatabaseExist(ConnectionString))
            {
                if (!DatabaseUtilities.DoesDatabaseMatchCurrentVersion(ConnectionString))
                {
                    DatabaseUtilities.CreateNewDatabase(ConnectionString);
                }
            }
            else
            {
                DatabaseUtilities.CreateNewDatabase(ConnectionString);
            }
        }

        /// <summary>
        /// Clears the database of its data
        /// </summary>
        public static void ClearDatabase()
        {
            var db = GetNewDataContext();
            {
                db.ExecuteCommand("DELETE FROM ForumServiceProviders");
                db.ExecuteCommand("DELETE FROM Users");
                db.ExecuteCommand("DELETE FROM PendingActions");
                db.ExecuteCommand("DELETE FROM PostDrafts");
            }
        }


        /// <summary>
        /// Creates and returns a new database context
        /// </summary>
        public static ForumsOfflineStore GetNewDataContext()
        {
            var context =  new ForumsOfflineStore(ConnectionString);
            DataLoadOptions dlo = new DataLoadOptions();
            dlo.LoadWith<Post>(p => p.PostPropertySet);
            dlo.LoadWith<Forum>(f => f.ForumPropertySet);
            context.LoadOptions = dlo;
            return context;
        }

        /// <summary>
        /// Updates the database with item.
        /// </summary>
        /// <typeparam name="T">Type of the item</typeparam>
        /// <param name="item">The item.</param>
        public static void UpdateDatabaseWithItem<T>(T item) where T : class
        {
            ForumsOfflineStore store = GetNewDataContext();
            var table = store.GetTable<T>();
            table.Attach(item);
            store.Refresh(RefreshMode.KeepCurrentValues, item);
            store.SubmitChangesWithMerge();
        }


        /// <summary>
        /// Inserts the item into database.
        /// </summary>
        /// <typeparam name="T">Type of the item</typeparam>
        /// <param name="item">The item.</param>
        public static void InsertItemIntoDatabase<T>(T item) where T : class
        {
            ForumsOfflineStore store = GetNewDataContext();
            var table = store.GetTable<T>();
            table.InsertOnSubmit(item);
            store.SubmitChangesWithMerge();
        }
    }
}
